package com.abss.vote.service.impl;

import com.abss.vote.mapper.*;
import com.abss.vote.model.*;
import com.abss.vote.service.RedisService;
import com.abss.vote.service.UserReceptionService;
import com.abss.vote.util.Download;
import com.abss.vote.util.FileUtils;
import com.abss.vote.util.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserReceptionServiceImpl implements UserReceptionService {

    @Autowired
    private ActivityMapper activityMapper;//活动
    @Autowired
    private DistrictListMapper districtListMapper;//地区
    @Autowired
    private PrizeMapper prizeMapper;//奖项
    @Autowired
    private UserMapper userMapper;//用户报名基本信息
    // @Autowired
    // private VotesMapper votesMapper;//投票
    @Autowired
    private WorkMapper workMapper;//作品
    @Autowired
    private LogoMapper logoMapper;
    @Autowired
    RedisService redisService;

    @Override
    public Activity getActivityId(int ACTIVITY_ID) {
        Activity ac = activityMapper.getActivityId(ACTIVITY_ID);



        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
        ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
        long timeremain=ac.getACTIVITY_ENDTIME().getTime()-System.currentTimeMillis();
        ac.setTIME_REMAINING(timeremain<0?0:timeremain);
        long startMilli=ac.getACTIVITY_STARTTIME().getTime();//开始时间
        long endMilli=ac.getACTIVITY_ENDTIME().getTime();//结束时间
        ac.setTIME_PROGRESS((endMilli-startMilli));
        if(new Date().before(ac.getACTIVITY_STARTTIME())){//在开始之前
            ac.setFalg(1);
        }else if(ac.getACTIVITY_ENDTIME().before(new Date())){//结束之后
            ac.setFalg(2);
        }else {
            ac.setFalg(0);
        }
        if(ac.getSTAGE_IS_OPEN()==1) {//开启阶段
            if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
            }
            if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
            }
            if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
            }
            if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
            }
        }
        return ac;
    }

    @Override
    public List<DistrictList> getCounty() {
        return districtListMapper.getCounty();
    }

    @Override
    public List<DistrictList> getCountyNoLook(String serverId,String admin) {
        List<DistrictList> lists=districtListMapper.getCountyById(1);
        if("100001".equals(serverId) && "feiyuebook".equals(admin)){//超级管理员
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictList> ad1= districtListMapper.getCountyid(lists.get(0).getID());//第一等级
            for(DistrictList aa: ad1 ){
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictList> ad2= districtListMapper.getCountyid(aa.getID());//第二等级
                for(DistrictList ab: ad2){
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictList> ad3= districtListMapper.getCountyid(ab.getID());//第三等级
                    for(DistrictList ac: ad3){
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictList> ad4= districtListMapper.getCountyid(ac.getID());//第四等级
                        for(DistrictList ad: ad4){
                            //将这一等级添加到lists
                            if(ad!=null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }

        }else{
            //根据自己的等级获取下一等级的所有管理员
            List<DistrictList> ad1= districtListMapper.getCountyidab(lists.get(0).getID(),serverId,admin);//第一等级
            for(DistrictList aa: ad1 ){
                //将这一等级添加到lists
                lists.add(aa);
                List<DistrictList> ad2= districtListMapper.getCountyidaa(aa.getID(),serverId);//第二等级
                for(DistrictList ab: ad2){
                    //将这一等级添加到lists
                    lists.add(ab);
                    List<DistrictList> ad3= districtListMapper.getCountyidaa(ab.getID(),serverId);//第三等级
                    for(DistrictList ac: ad3){
                        //将这一等级添加到lists
                        lists.add(ac);
                        List<DistrictList> ad4= districtListMapper.getCountyidaa(ac.getID(),serverId);//第四等级
                        for(DistrictList ad: ad4){
                            //将这一等级添加到lists
                            if(ad!=null) {
                                lists.add(ad);
                            }
                        }
                    }
                }
            }
        }
        lists.remove(0);//除去超级管理员
        for (int i =0;i<lists.size();i++){
            if(lists.get(i).getIS_DISPLAY()==1){
                lists.remove(i);
            }
        }
        return lists;
    }

    @Override
    public int verification(String TELEPHONE,int ACTIVITY_ID) {
        return userMapper.verification(TELEPHONE,ACTIVITY_ID);
    }

    @Override
    @Transactional
    public int singUp(UserWorkVo userWorkVo) {
        if(!userWorkVo.getSOUND().equals(null)){//如果有音频，单独添加音频
            userWorkVo.setWORK_TYPE("1");
            workMapper.addSound(userWorkVo);
        }
        if(!userWorkVo.getVIDEO().equals(null)){//如果有视频，单独添加视频
            userWorkVo.setWORK_TYPE("0");
            workMapper.addVideo(userWorkVo);
        }
        int res=userMapper.singUp(userWorkVo);//添加进用户报名表
        return res;
    }

    @Override
    public int checkSingUp(int ACTIVITY_ID,String USER_ID) {
        return userMapper.checkSingUp(ACTIVITY_ID, USER_ID);
    }

    @Override
    public UserWorkVo getSingUp(UserWorkVo userWorkVo) {
        //先查询出用户信息
        UserWorkVo uWV = userMapper.getUser(userWorkVo);
        //查询对应作品信息（最多有两条）
        List<Work> works= workMapper.getUserWorks(userWorkVo);
        for(int i = 0; i < works.size() ; i++){
            String type = works.get(i).getWORK_TYPE();
            if("0".equals(type)){//0视频
                uWV.setVIDEO(works.get(i).getRESOURCE_URL());
            }else{//1音频
                uWV.setSOUND(works.get(i).getRESOURCE_URL());
            }
        }
        return uWV;
    }

    @Override
    @Transactional
    public int updateSingUp(UserWorkVo userWorkVo) {
        if(!userWorkVo.getSOUND().equals(null)){//如果有音频，单独修改音频
            userWorkVo.setWORK_TYPE("1");
            workMapper.updateSound(userWorkVo);
        }
        if(!userWorkVo.getVIDEO().equals(null)){//如果有视频，单独修改视频
            userWorkVo.setWORK_TYPE("0");
            workMapper.updateVideo(userWorkVo);
        }
        return userMapper.updateSingUp(userWorkVo);//修改用户报名表

    }

    @Override
    public ActivityPrizeVo getPrize1(int ACTIVITY_ID) {
        return activityMapper.getPrize1(ACTIVITY_ID);
    }
    @Override
    public ActivityPrizeVo getPrize2(int ACTIVITY_ID) {
        return activityMapper.getPrize2(ACTIVITY_ID);
    }
    @Override
    public ActivityPrizeVo getPrize3(int ACTIVITY_ID) {
        return activityMapper.getPrize3(ACTIVITY_ID);
    }

    @Override
    public int setVisist(int ACTIVITY_ID) {
        return activityMapper.setVisist(ACTIVITY_ID);
    }

    @Override
    @Transactional
    public Map<String,Object> setVOTES(int ACTIVITY_ID, String USER_ID, int WORK_ID) {
        Map<String,Object> ret=new HashMap<String,Object>();
        if("".equals(USER_ID)||USER_ID==null){
            ret.put("code",0);
            ret.put("msg","正在加载···");
            return ret;
        }
        User user = userMapper.checkUserInfo(USER_ID);//new User();
        if(user!=null) {
            user.setACTIVITY_ID(ACTIVITY_ID);//获取用户id
            user.setUSER_ID(USER_ID);
            ActivityWorkVo activityWorkVo = new ActivityWorkVo();
            activityWorkVo.setACTIVITY_ID(ACTIVITY_ID);
            activityWorkVo.setUSER_ID(USER_ID);
            activityWorkVo.setWORK_ID(WORK_ID);
            //从redis中获取限制点赞数值
            int LEFT_VOTES = redisService.getVOTES_LIMIT_PERDAY(ACTIVITY_ID);//获取将该活动设置的点赞次数
            int PERDAY = redisService.getVOTES_LIMIT_TOSINGLE_PERDAY(ACTIVITY_ID);//获取每天对同一个人最多多少票
            //获取该用户的投票信息
            UserVoteInfo userVoteInfo = redisService.getUSERVOTESINFO(ACTIVITY_ID, USER_ID, WORK_ID);
            if (userVoteInfo.getAllVoteNumber() > 0) {//该用户今天投过票
                if (userVoteInfo.getAllVoteNumber() < LEFT_VOTES) {//还有剩余票数
                    if (userVoteInfo.getWorkVoteNumber() < PERDAY) {//对同一作品还有剩余票数
                        redisService.addVote(ACTIVITY_ID, USER_ID, WORK_ID);
                        ret.put("code", 1);
                        ret.put("msg", "投票成功！");
                    } else {
                        ret.put("code", 2);
                        ret.put("msg", "今日对该作品投票数已用光！");
                    }
                } else {
                    ret.put("code", 3);
                    ret.put("msg", "今日投票数已用光！");
                }
            } else {//该用户今天没有投过票
                //设置活动总投票数加一
                redisService.addVote(ACTIVITY_ID, USER_ID, WORK_ID);
                //在用户点赞信息表减一(添加时已减)
                //该活动的投票总数加一(投票人数已加)
                ret.put("code", 1);
                ret.put("msg", "投票成功！");
            }
        }else{
            ret.put("code", 4);
            ret.put("msg", "请先登录！");
        }
        return ret;

    }

    @Override
    public List<DistrictListUserWorkVo> getWorkByUserName(String NAME, int ACTIVITY_ID) {
        //首先获取作品信息
        List<DistrictListUserWorkVo> list =userMapper.getWorkByUserName(NAME,ACTIVITY_ID);
        for(int i=0;i<list.size();i++){
            int DISTRICT_ID=list.get(i).getDISTRICT_ID();
            String Name=districtListMapper.getCountyName(DISTRICT_ID);
            list.get(i).setDISTRICT_NAME(Name);
        }
        return list;
    }

    @Override
    public List<DistrictListUserWorkVo> getWorkByVotesNumber() {
        //首先获取作品信息
        List<DistrictListUserWorkVo> list =userMapper.getWorkByVotesNumber();
        for(int i=0;i<list.size();i++){
            int DISTRICT_ID=list.get(i).getDISTRICT_ID();
            String Name=districtListMapper.getCountyName(DISTRICT_ID);
            list.get(i).setDISTRICT_NAME(Name);
        }
        return list;
    }

    @Override
    public DistrictListUserWorkVo getWorkDetails(int WORK_ID) {
        DistrictListUserWorkVo dis=workMapper.getWorkDetails(WORK_ID);
        int ID = dis.getDISTRICT_ID();
        String name = districtListMapper.getCountyName(ID);
        dis.setDISTRICT_NAME(name);
        return dis;
    }

    @Override
    @Transactional
    public int addUser(userCallBack userCallBack) {
        return userMapper.addUser(userCallBack);//添加用户
    }

    @Override
    public int checkUser(userCallBack user) {
        return userMapper.checkUser(user);
    }

    @Override
    @Transactional
    public int updateUser(userCallBack userCallBack) {
         return userMapper.updateUser(userCallBack);
    }

    @Override
    @Transactional
    public int updaUserVoteInfo(User user) {
        User u= userMapper.getUserVoteInfo(user);//查询投票信息表中的记录
        if(u==null){//该用户未添加投票信息，添加
            return userMapper.addUserVotes(user);
        }else{//该用户已有投票信息，去修改
            //获取该用户下的剩余投票数量以及最后投票时间
            Date Lut = u.getLAST_UPDATE_TIME();//最后投票时间
            //如果最后投票时间在今天之前，进行修改信息
            Date time = Utils.getDate();//获取今天凌晨时间
            if(Lut.after(time)){//如果最后投票时间在今天之前，进行修改信息   判断开始时间是否在结束时间之后,返回布尔值 是true   否false
                return userMapper.updateUserVotesInfo(user);
            }
        }
        return 0;
    }

    @Override
    public User getUser(String USER_ID) {
        User user;
        List<User> us =  userMapper.getUserInfo(USER_ID);
        user=us.get(0);
        return user;
    }

    @Override
    public PageInfo<Activity> getActivityBySth(int start, int end) {
        PageHelper.startPage(start,end);
        List<Activity> list = activityMapper.getActivityBySth();//条件获取活动
        for(Activity ac:list){
            int ACTIVITY_ID = ac.getACTIVITY_ID();
            ac.setAutherNumber(userMapper.getVoteUserNum(ACTIVITY_ID));//根据活动ID获取点赞人数
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
            ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
            if(ac.getSTAGE_IS_OPEN()==1) {//开启阶段
                if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                    ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                    ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
                }
                if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                    ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                    ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
                }
                if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                    ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                    ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
                }
                if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                    ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                    ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
                }
            }
        }
        PageInfo<Activity> Goods= new PageInfo<>(list);
        return Goods;
    }

    @Override
    public List<DistrictListUserWorkVo> getWorkByActivityId(int ACTIVITY_ID) {
        return workMapper.getWorksByActivityID(ACTIVITY_ID);
    }

    @Override
    public List<Activity> getActivities(int start, int end,String ServerId,String admin) {

        List<Activity> result = null;
        if("100001".equals(ServerId)){//爱不释书服务器查看所有的活动
            if("feiyuebook".equals(admin)){//爱不释书查看
                result=activityMapper.getActivits(start,end);
            }else {//其他查看
                result = activityMapper.getActivitss(start, end,admin);
            }
        }else{//别的服务器查看自己服务器和爱不释书发布的活动
            result=activityMapper.getActivit(start,end,ServerId);
        }
        for(Activity ac:result){
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ac.setSTARTTIME(formatter.format(ac.getACTIVITY_STARTTIME()));
            ac.setENDTIME(formatter.format(ac.getACTIVITY_ENDTIME()));
            if(Integer.valueOf(1).equals(ac.getSTAGE_IS_OPEN())) {//开启阶段
                if(ac.getSTAGE_1ST_ENDTIME()!=null) {
                    ac.setST1_ENDTIME(formatter.format(ac.getSTAGE_1ST_ENDTIME()));
                    ac.setST1_STARTTIME(formatter.format(ac.getSTAGE_1ST_STARTTIME()));
                }
                if(ac.getSTAGE_2ND_ENDTIME()!=null) {
                    ac.setST2_ENDTIME(formatter.format(ac.getSTAGE_2ND_ENDTIME()));
                    ac.setST2_STARTTIME(formatter.format(ac.getSTAGE_2ND_STARTTIME()));
                }
                if(ac.getSTAGE_3RD_ENDTIME()!=null) {
                    ac.setST3_ENDTIME(formatter.format(ac.getSTAGE_3RD_ENDTIME()));
                    ac.setST3_STARTTIME(formatter.format(ac.getSTAGE_3RD_STARTTIME()));
                }
                if(ac.getSTAGE_4TH_ENDTIME()!=null) {
                    ac.setST4_ENDTIME(formatter.format(ac.getSTAGE_4TH_ENDTIME()));
                    ac.setST4_STARTTIME(formatter.format(ac.getSTAGE_4TH_STARTTIME()));
                }
            }
        }
        return result;
    }

    @Override
    public List<Work> getWorksWithOrder(int activity_id,int start, int end, int order_type,int currentStage) {
        //按投票排
        List<Work> works=new ArrayList<>();
        if(order_type==2){
            works=workMapper.getWorksOrderByRandom(activity_id,start,end,currentStage);
        }else if(order_type==1){
            works=workMapper.getWorksOrderByNewUpload(activity_id,start,end,currentStage);

        }else{
            if(currentStage==0){//审核通过前（包括审核通过，即刚上传的作品和审核通过的作品，不包括审核未通过的作品）
                works=workMapper.getWorksAudit(activity_id,start,end);
            }else if(currentStage==1){//只显示管理员提交的作品
                works=workMapper.getWorksSubmit(activity_id,start,end);
            }else if(currentStage==2){//只显示活动发布者晋级的作品
                works=workMapper.getWorksPromotion(activity_id,start,end);
            }


        }
        for(Work work:works){
            work.setVOTES_NUMBER(redisService.getWOrkVotesNumber(activity_id,work.getWORK_ID()));
        }
        Collections.sort(works);
        List<Work> work = new ArrayList<>();
        if((start-1)==works.size()){//没有数据返回null
            return work;
        }else if((start-1)<works.size() && (end-1)>=works.size()){//获取的数据不够
            work = works.subList((start-1),works.size());
        }else if((end-1)<works.size()){
            work = works.subList((start-1),(end-1));
        }

        return work;

    }

    @Override
    public List<Work> getWorksWithOrderAndSearch(String search_user_name,Integer search_work_id,int district_id,String search_content, int activity_id, int start, int end, int order_type,int currentStage) {

        //按投票排
        List<Work> works=new ArrayList<>();
        if(order_type==2){
            works=workMapper.getSearchedWorksOrderByRandom(district_id,search_content,activity_id,start,end,currentStage);
        }else if(order_type==1){
            works=workMapper.getSearchedWorksOrderByNewUpload(district_id,search_content,activity_id,start,end,currentStage);

        }else{
            if(currentStage==0){//审核通过前（包括审核通过，即刚上传的作品和审核通过的作品，不包括审核未通过的作品）
                works=workMapper.getSearchedWorksOrderByVotesNumOneAuditQ(search_user_name,search_work_id,search_content,activity_id,district_id);
//                if(district_id==0){//没有地址
//                    works=workMapper.getSearchedWorksOrderByVotesNumOneAudit(search_content,activity_id,start,end);
//                }else if(search_content==null||"".equals(search_content.trim())){//没有名字
//                    works=workMapper.getSearchedWorksOrderByVotesNumTwoAudit(district_id,activity_id,start,end);
//                }else {//全有
//                    works=workMapper.getSearchedWorksOrderByVotesNumAudit(district_id,search_content,activity_id,start,end);
//                }
            }else if(currentStage==1){//只显示管理员提交的作品
                works=workMapper.getSearchedWorksOrderByVotesNumOneSubmitQ(search_user_name,search_work_id,search_content,activity_id,district_id);
//                if(district_id==0){//没有地址
//                    works=workMapper.getSearchedWorksOrderByVotesNumOneSubmit(search_content,activity_id,start,end);
//                }else if(search_content==null||"".equals(search_content.trim())){//没有名字
//                    works=workMapper.getSearchedWorksOrderByVotesNumTwoSubmit(district_id,activity_id,start,end);
//                }else {//全有
//                    works=workMapper.getSearchedWorksOrderByVotesNumSubmit(district_id,search_content,activity_id,start,end);
//                }
            }else if(currentStage==2){//只显示活动发布者晋级的作品
                works=workMapper.getSearchedWorksOrderByVotesNumOnePromotionQ(search_user_name,search_work_id,search_content,activity_id,district_id);
//                if(district_id==0){//没有地址
//                    works=workMapper.getSearchedWorksOrderByVotesNumOnePromotion(search_content,activity_id,start,end);
//                }else if(search_content==null||"".equals(search_content.trim())){//没有名字
//                    works=workMapper.getSearchedWorksOrderByVotesNumTwoPromotion(district_id,activity_id,start,end);
//                }else {//全有
//                    works=workMapper.getSearchedWorksOrderByVotesNumPromotion(district_id,search_content,activity_id,start,end);
//                }
            }
        }
        for(Work work:works){
            work.setVOTES_NUMBER(redisService.getWOrkVotesNumber(activity_id,work.getWORK_ID()));
        }
        Collections.sort(works);
        List<Work> work = new ArrayList<>();
        if((start-1)==works.size()){//没有数据返回null
            return work;
        }else if((start-1)<works.size() && (end-1)>=works.size()){//获取的数据不够
            work = works.subList((start-1),works.size());
        }else if((end-1)<works.size()){
            work = works.subList((start-1),(end-1));
        }

        return work;
    }

    @Override
    public Prize getPrize1Info(int activity_id) {
        return activityMapper.getPrize1Info(activity_id);
    }
    @Override
    public Prize getPrize2Info(int activity_id) {
        return activityMapper.getPrize2Info(activity_id);
    }
    @Override
    public Prize getPrize3Info(int activity_id) {
        return activityMapper.getPrize3Info(activity_id);
    }

    @Override
    @Transactional
    public Work getWorkInfoById(int work_id) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        Work work = workMapper.getWorkInfoById(work_id);
        work.setTIME(formatter.format(work.getWORK_UPTIME()));
        if("2".equals(work.getWORK_TYPE())){
            String[] URL = work.getRESOURCE_URL().split(",");//分割出来的字符数组
            List<Map<String ,String >> list = new ArrayList<>();
            for(int i = 0; i<URL.length;i++){
                Map<String,String> map =new HashMap<String, String>();
                map.put("url",URL[i]);
                list.add(map);
            }
            work.setURL(list);
        }
        work.setVOTES_NUMBER(redisService.getWOrkVotesNumber(work.getACTIVITY_ID(),work_id));
        return work;
    }

    @Override
    @Transactional
    public int delUser(String user_id, int activity_id) {
        //查询是否有作品
        int res = workMapper.getWorkisempty(user_id,activity_id);
        if(res>0) {
           workMapper.delWorks(user_id, activity_id);
        }
        return userMapper.delUser(user_id,activity_id);
    }

    @Override
    @Transactional
    public int addUserRegInfo(UserRegInfo userRegInfo) {
        return userMapper.addUserRegInfo(userRegInfo);
    }

    @Override
    @Transactional
    public int updUserRegInfo(UserRegInfo userRegInfo) {
        return userMapper.updUserRegInfo(userRegInfo);
    }

    @Override
    public UserRegInfo getUserRegInfo(String user_id, int activity_id) {
        return userMapper.getUserReg(user_id,activity_id);
    }

    @Override
    @Transactional
    public int addyinWork(Work work) {
        return workMapper.addyinWork(work);
    }

    @Override
    @Transactional
    public int delWork(int work_id) {
        return workMapper.delWork(work_id);
    }

    @Override
    @Transactional
    public int updSound(Work work) {
        return workMapper.updSound(work);
    }

    @Override
    @Transactional
    public int updSounds(Work work) {
        return workMapper.updSounds(work);
    }

    @Override
    public Work getWork(int work_id) {
        return workMapper.getWORKS(work_id);
    }

    @Override
    public Work getWorks(int work_id) {
        Work work =  workMapper.getWORKS(work_id);
        String[] URL = work.getRESOURCE_URL().split(",");//分割出来的字符数组
        List<Map<String ,String >> list = new ArrayList<>();
        for(int i = 0; i<URL.length;i++){
            Map<String,String> map =new HashMap<String, String>();
            map.put("url",URL[i]);
            list.add(map);
        }
        work.setURL(list);
        return work;
    }

    @Override
    public PrizeUser getWorksp(int work_id,int ACTIVITY_ID) {
        PrizeUser prizeUser = workMapper.getPrizeUser(ACTIVITY_ID);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(prizeUser!=null) {
            if (prizeUser.getSTART_TIME() != null && prizeUser.getEND_TIME() != null) {
                prizeUser.setSTART(formatter.format(prizeUser.getSTART_TIME()));
                prizeUser.setEND(formatter.format(prizeUser.getEND_TIME()));
            }
        }
        if(prizeUser==null){
            prizeUser = new PrizeUser();
        }
        Work work = workMapper.getWORKS(work_id);
        if(work.getPRIZE_ID()!=null) {
            String Prize_name = prizeMapper.getPrizeName(work.getPRIZE_ID());
            prizeUser.setPRIZE_ID(work.getPRIZE_ID());
            prizeUser.setPRIZE_NAME(Prize_name);
        }
        String activity_name = activityMapper.getActivityName(ACTIVITY_ID);
        prizeUser.setACTIVITY_NAME(activity_name);
        prizeUser.setWORK_ID(work_id);
        prizeUser.setWORK_NAME(work.getWORK_NAME());
        prizeUser.setIS_AWARD(work.getIS_AWARD());
        return prizeUser;
    }



    @Override
    public List<Work> getWorksByUserIdAndActivityId(String user_id, int activity_id) {
        return workMapper.getWorksByUserIdAndActivityId(user_id,activity_id);
    }

    @Override
    public List<Article> getReadWork() {
        return workMapper.getReadWork();
    }

    @Override
    public Article getActicleById(int ID) {
        return workMapper.getActicleById(ID);
    }

    @Override
    @Transactional
    public int addRecorderWork(Work work) {
        return workMapper.addRecorderWork(work);
    }

    @Override
    public int getCurrentStage(int activity_id) {
        return activityMapper.getCurrentStage(activity_id);
    }

    @Override
    public MpInfo getMpInFo(String serverId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        MpInfo mpInfo = prizeMapper.getMpInFo(serverId);
        try {
            mpInfo.setLAST_UPDATE_TIME(sdf.parse(mpInfo.getLast_time()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return mpInfo;
    }

    @Override
    public int setMpInFo(MpInfo mpInfo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String ACCESS_TOKEN = mpInfo.getACCESS_TOKEN();
        String MP_APP_ID = mpInfo.getMP_APP_ID();
        String MP_APP_SECRET = mpInfo.getMP_APP_SECRET();
        String last_update_milliseconds = String.valueOf(new Date().getTime());
        String last_update_time = sdf.format(new Date());
        prizeMapper.setMpInFos(ACCESS_TOKEN,MP_APP_ID,MP_APP_SECRET,last_update_time,last_update_milliseconds);
        return prizeMapper.setMpInFo(ACCESS_TOKEN,MP_APP_ID,MP_APP_SECRET,new Date());
    }

    @Override
    public int setMpInFoticket(MpInfo mpInfo1) {
        return prizeMapper.setMpInFocs(mpInfo1);
    }

    @Override
    public List<DistrictList> getCountyHaveAdmin() {
        //获取全部管理员（除超级管理员外）
        List<DistrictList> lists=districtListMapper.getCountyById(1);
        //根据自己的等级获取下一等级的所有管理员
        List<DistrictList> ad1= districtListMapper.getCountyid(lists.get(0).getID());//第一等级
        for(DistrictList aa: ad1 ){
            //将这一等级添加到lists
            lists.add(aa);
            List<DistrictList> ad2= districtListMapper.getCountyid(aa.getID());//第二等级
            for(DistrictList ab: ad2){
                //将这一等级添加到lists
                lists.add(ab);
                List<DistrictList> ad3= districtListMapper.getCountyid(ab.getID());//第三等级
                for(DistrictList ac: ad3){
                    //将这一等级添加到lists
                    lists.add(ac);
                    List<DistrictList> ad4= districtListMapper.getCountyid(ac.getID());//第四等级
                    for(DistrictList ad: ad4){
                        //将这一等级添加到lists
                        if(ad!=null) {
                            lists.add(ad);
                        }
                    }
                }
            }
        }
        lists.remove(0);//除去超级管理员
        return lists;
    }

    @Override
    public Logo getLogos(String admin) {
        return logoMapper.getLogos(admin);
    }

    @Override
    public List<UserWorkVo> getPrizeOne(Integer prize1_id) {
        List<UserWorkVo> list = prizeMapper.getPrizeOne(prize1_id);
        if(list.size()==0){
            UserWorkVo userWorkVo = new UserWorkVo();
            userWorkVo.setPRIZE_NAME(prizeMapper.getPrizeName(prize1_id));
            list.add(userWorkVo);
        }
        return list;
    }



    @Override
    public String getAdmin(String server_id) {
        return logoMapper.getAdmin(server_id);
    }


    @Override
    public int getProbation(String server_id, String admin) {
        Probation probation = logoMapper.getProbationByadmin(server_id, admin);
        if(probation!=null){
            return probation.getIS_PROBTATION();
        }
        return 0;
    }

    @Override
    public String getImageFromWeixin(String mediaId,MpInfo mp) {

        //http请求方式: GET,https调用 https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
        // 请求示例（示例为通过 curl 命令获取多媒体文件） curl -I -G "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"

        //参数说明

//        参数	是否必须	说明
//        access_token	是	调用接口凭证
//        media_id	是	媒体文件ID

        long time1=Utils.getName();//获取当前时间戳
        String name = time1+ ".jpg";
        String localpath=mp.getFILE_PATH() + "/copy/"+name;

        String serverpath="https://api.weixin.qq.com/cgi-bin/media/get?access_token="+mp.getACCESS_TOKEN()+"&media_id="+mediaId;

        System.out.println("localPath:"+localpath);
        System.out.println("serverPath:"+serverpath);


        Download.downloadPicture(serverpath,localpath);








        return FileUtils.saveImage(new File(localpath),name,mp);





    }
}
